var myTool = (function(win, doc){
    /* 
        获取非行间（计算后， 当前）样式函数封装
        @params el: element  attr: string
    */
            
    function getStyle(el, attr){ 
        return win.getComputedStyle ? getComputedStyle(el)[attr] : el.currentStyle[attr];
    }

    /* 
        匀速运动函数
        @params: el:element  attr:string  target:number  step:number
    */
    var timer = null;
    function linearMove(el, attr, target, step){
        step = target > parseInt(getStyle(el, attr)) ? step : -step;
        clearInterval(timer);
        timer = setInterval(function(){
            var cur = parseInt(getStyle(el, attr));
            var speed = cur + step;
            if(step > 0 && speed >= target || step < 0 && speed <= target){
                speed = target;
                clearInterval(timer);
            }
            el.style[attr] = speed + 'px';
        }, 20)
    }

    /* 
        获取随机n位验证码
        @parms: n: number 
    */

    function checkCode(n){
        var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var code = '';
        for(var i = 0; i < n; i++){
            var char = str[Math.floor(Math.random() * str.length)];
            code += char;
        }
        return code;
    }

    /* 
        计算深度为n的元素到文档顶部的距离
        @params: el:element
    */
    function getTop(el){
        var iTop = 0;
        while(el.offsetParent){
            iTop += el.offsetTop;
            el = el.offsetParent;
        }

        return iTop;
    }

    /* 
        事件绑定兼容函数
        @params: el:element  oEvent: string  fn: function
    */

    function bindEvent(el, oEvent, fn){
        el.addEventListener ? el.addEventListener(oEvent, fn, false) : el.attachEvent('on' + oEvent, fn);
    }

    /* 
        事件取消绑定兼容函数
        @params: el:element  oEvent: string  fn: function
    */

    function unbind(el, oEvent, fn){
        el.removeEventListener ? el.removeEventListener(oEvent, fn, false) : el.detachEvent('on' + oEvent, fn);
    }

    /* 
        缓冲运动函数
        @params  el:element  obj:object  属性和值的组合  fn:function  动画完成后的回调函数
    */
    function bufferMove(el, obj, fn){
        clearInterval(el.timer);
        el.timer = setInterval(function(){
            var flag = true;
            for(var attr in obj){
                if(attr == 'opacity'){
                    var cur = Math.round(getStyle(el, attr) * 100); 
                }else{
                    // 非透明度的属性
                    var cur = parseInt(getStyle(el, attr));
                }
                var step = (obj[attr] - cur) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                
                if(cur != obj[attr]){
                    flag = false;
                }
        
                if(attr == 'opacity'){
                    el.style.opacity = (cur + step) / 100;
                    el.style.filter = 'alpha(opacity=' + (cur + step) + ')';
                }else{
                    el.style[attr] = cur + step + 'px';
                }
            }

            if(flag){
                clearInterval(el.timer);
                if(fn) fn.call(el);//如果有传fn 那么执行 fn
                // fn && fn();
            }
        }, 20)
    }

    return {
       'getStyle' : getStyle,
       'linearMove': linearMove,
       'checkCode': checkCode,
       'getTop': getTop,
       'bindEvent': bindEvent, 
       'unbind': unbind,
       'bufferMove': bufferMove
    }
})(window, document);


/* 
    var myTool = {
       'getStyle' : getStyle,
       'linearMove': linearMove,
       'checkCode': checkCode,
       'getTop': getTop,
       'bindEvent': bindEvent, 
       'unbind': unbind,
       'bufferMove': bufferMove
    }

    myTool.getStyle();

    myTool.bufferMove();
*/